package com.vpx.server.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.SysDicType;
import com.vpx.server.bean.enums.AbleDisableEnum;
import com.vpx.server.bean.info.system.SysDicTypeInfo;
import com.vpx.server.bean.order.base.BaseIdOrder;
import com.vpx.server.bean.order.system.SysDicTypeOrder;
import com.vpx.server.bean.order.system.SysDicTypePageOrder;
import com.vpx.server.bean.result.system.SysDicTypePageResult;
import com.vpx.server.bean.result.system.SysDicTypeResult;
import com.vpx.server.framework.aop.ServiceHandler;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.exception.BizException;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.mapper.system.SysDicTypeMapper;
import com.vpx.server.mapper.system.SysDicValueMapper;
import com.vpx.server.service.system.SysDicTypeService;
import com.vpx.server.utils.UserLoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module: 数据字典类型
 * @description: 数据字典类型服务接口实现
 * @date 2021/09/20 17:33
 */
@Service
public class SysDicTypeServiceImpl implements SysDicTypeService {

    @Autowired
    private SysDicTypeMapper sysDicTypeMapper;

    @Autowired
    private SysDicValueMapper sysDicValueMapper;


    @Override
    @ServiceHandler(memo = "新增数据字典类型", isTransaction = true)
    public ResultMsg insert(SysDicTypeOrder order) {
        int count = sysDicTypeMapper.querySysDicTypeByCode(order.getCode());
        if (count > 0){
            throw new BizException("编码已存在");
        }
        SysDicType dicType = new SysDicType();
        dicType.from(order);
        dicType.setStatus(AbleDisableEnum.ABLE.code());
        dicType.setCreateUserId(UserLoginUtils.getCurrentUser().getId());
        dicType.setCreateTime(DateUtils.now());
        sysDicTypeMapper.insert(dicType);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "修改数据字典类型", isTransaction = true)
    public ResultMsg updateById(SysDicTypeOrder order) {
        SysDicType dicType = sysDicTypeMapper.querySysDicTypeById(order.getId());
        if (dicType == null){
            throw new BizException("未查询到数据字典类型");
        }
        if (checkCodeIsExists(order)){
            throw new BizException("编码已存在");
        }
        dicType.from(order);
        sysDicTypeMapper.updateById(dicType);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "删除数据字典类型", isTransaction = true)
    public ResultMsg deleteById(BaseIdOrder order) {
        sysDicTypeMapper.updateDicTypeStatusById(order.getId(),AbleDisableEnum.DISABLE.code());
        sysDicValueMapper.updateSysDicValueByTypeId(order.getId(),AbleDisableEnum.DISABLE.code());
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "查询数据字典类型")
    public ResultMsg<SysDicTypeResult> findById(BaseIdOrder order) {
        SysDicType dicType = sysDicTypeMapper.querySysDicTypeById(order.getId());
        if (dicType == null){
            throw new BizException("未查询到数据字典类型");
        }
        SysDicTypeResult result = new SysDicTypeResult();
        result.from(dicType);
        return ResultMsg.success(result);
    }

    @Override
    @ServiceHandler(memo = "分页查询数据字典类型")
    public ResultMsg<SysDicTypePageResult> pageQuery(SysDicTypePageOrder order) {
        if (order.getPageNum() != null && order.getPageSize() != null) {
            PageHelper.startPage(order.getPageNum(), order.getPageSize());
        }
        List<SysDicTypeInfo> dicTypeInfos = sysDicTypeMapper.pageQuery(order);
        PageInfo<SysDicTypeInfo> pageInfo = new PageInfo<SysDicTypeInfo>(dicTypeInfos);
        SysDicTypePageResult result = new SysDicTypePageResult();
        result.setPageInfo(pageInfo);
        return ResultMsg.success(result);
    }

    @ServiceHandler(memo = "检查编码是否存在")
    public boolean checkCodeIsExists(SysDicTypeOrder order){
        int count = sysDicTypeMapper.querySysDicTypeByIdAndCode(order.getId(),order.getCode());
        if(count == 1){
            return false;
        }
        int num = sysDicTypeMapper.querySysDicTypeByCode(order.getCode());
        if(num > 0){
            return true;
        }
        return false;
    }
}
